<?php

/**
 * I am a JSON Query builder
 *
 */
class JSONQuery
{
	private $dbconn;
	private $jsonArray;
	private $jsonString;
	private $jsonQuery;
	
	/**
	 * Database Link
	 *
	 * @param unknown_type $link
	 */
	public function __construct( $link )
	{
		$this->dbconn = $link;
	}
	
	public function buildQuery( $json, $type )
	{
		return $this->_buildQuery ( $json, $type );
	}
	
	private function _buildQuery( $json, $type )
	{
		$jsonString = str_replace ( "\\", "", $json );
		$jsonArray = json_decode ( $jsonString, true );
		$query = '';
		$queryArray = array ();
		
		switch ( $type )
		{
			case 'INSERT' :
				//do insert statement
				foreach ( $jsonArray as $record )
				{
					$query = sprintf ( 'INSERT INTO %s.%s SET %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'objectName' ], $record [ 'objectValue' ] );
					//echo $query;
					$queryArray [] = $query;
				}
				
				break;
			
			case 'UPDATE' :
				
				foreach ( $jsonArray as $record )
				{
					$query = sprintf ( 'UPDATE %s.%s SET %s = "%s" WHERE %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'objectName' ], $record [ 'objectValue' ], $record [ 'tableKey' ], $record [ 'objectKey' ] );
					//echo $query;
					$queryArray [] = $query;
				}
				
				break;
			
			case 'DELETE' :
				
				foreach ( $jsonArray as $record )
				{
					$query = sprintf ( 'DELETE FROM %s.%s WHERE %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'tableKey' ], $record [ 'objectKey' ] );
					//echo $query;
					$queryArray [] = $query;
				}
				
				break;
			
			default :
				echo 'Specify a type of statement';
				break;
		}
		
		return $this->_batchQuery ( $queryArray );
	}
	
	private function _batchQuery( $sql )
	{
		foreach ( $sql as $q )
		{
			$result = $this->dbconn->query ( $q );
			if ( $result )
			{
				printf ( "%d Record removed\n", $this->dbconn->affected_rows );
			}
		}
	}
	
	public function _filterTable( $s )
	{
		$tablename = stripos ( $s, 'table' );
		
		return $tablename;
	}
	
	/**
	 * @return associative array
	 */
	public function getJSONArray()
	{
		return $this->jsonArray;
	}
	
	/**
	 * @return json string
	 */
	public function getJSONString()
	{
		return $this->jsonString;
	}
	
	/**
	 * @return sql query
	 */
	public function getQuery()
	{
		return $this->query;
	}
	
	/**
	 * @param associative $jsonArray
	 */
	public function setJSONArray( $json )
	{
		$jsonArray = json_decode ( $json, true );
		
		$this->jsonArray = $jsonArray;
	}
	
	/**
	 * @param json $jsonString
	 */
	public function setJSONString( $jsonString )
	{
		$this->jsonString = $jsonString;
	}
	
	/**
	 * @param string $query
	 */
	public function setJSONQuery( $query )
	{
		$this->jsonQuery = $query;
	}
	
	/**
	 * I dump vars
	 *
	 * @param [var] $var
	 * @param [string] $name
	 */
	public function dump( $var, $name = 'Variable Dump' )
	{
		echo "<h4>$name</h4>";
		echo '<pre>';
		print_r ( $var );
		echo '</pre>';
	}
	
	/**
	 * Escape SQL 
	 *
	 * @param [string] $str
	 * @return escaped string
	 */
	private function _sqlQuote( $str )
	{
		if ( ! isset ( $str ) )
			return ( "NULL" );
		
		$func = function_exists ( "mysqli_escape_string" ) ? "mysqli_escape_string" : "addslashes";
		return ( "'" . $func ( $str ) . "'" );
	}

}

//$flexJSON = '[
//{"table":"contacts","objectName":"name","objectValue":"Jonnie","database":"test","tableKey":"id","objectKey":"1"},
//{"table":"contacts","objectName":"name","objectValue":"Sue","database":"test","tableKey":"id","objectKey":"2"},
//{"table":"contacts","objectName":"name","objectValue":"Hannah","database":"test","tableKey":"id","objectKey":"3"},
//{"table":"contacts","objectName":"name","objectValue":"Lauren","database":"test","tableKey":"id","objectKey":"4"},
//{"table":"contacts","objectName":"name","objectValue":"John","database":"test","tableKey":"id","objectKey":"5"},
//{"table":"contacts","objectName":"name","objectValue":"Rio","database":"test","tableKey":"id","objectKey":"6"},
//{"table":"contacts","objectName":"name","objectValue":"Tyrrell","database":"test","tableKey":"id","objectKey":"7"}
//]
//';


//$j = new JSONQuery( 'localhost', 'root', 'fred' );


//$j->dump( $j->buildQuery( $flexJSON, 'INSERT' ), 'INSERT JSON ' );
//$j->dump( $j->buildQuery( $flexJSON, 'UPDATE' ), 'UPDATE JSON' );
//$j->dump( $j->buildQuery( $flexJSON, 'DELETE' ), 'DELETE JSON' );


//$j->setJSONString( $flexJSON );
//$j->dump( $j->getJSONString(), 'JSON STRING' );


?>